<?php
    include_once './config.php';
    $user = checkLogin(3);
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $type = $_POST['type'];
        if($type == 1){ //退出登录
            setcookie("user", '', 0);
            $msg = "退出成功";
        } else {
            $query = "update whw_user set nickname = '',avatar = '',openid = '' where id=".getInt($user['id']);
            $objDBCD14->execute($query);
            $msg = "解绑微信成功";
        }
        echo json_encode(['code'=>$error ? 0 : 1,'msg'=>$msg]);
        exit;
    }
    $faceList = [];
    $user_id = $_GET['user_id'];
    $query = "select * from whw_face where user_id=".getInt($user_id);
    $rows = $objDBCD14->query($query);
    if ($rows->num_rows) {
        foreach ($rows as $key => $face) {
            $faceList[] =  $face; 
        }  
    }
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>设置</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>
        .container{
            margin: 0;
            padding: 10px 10px 20px;
            box-sizing: border-box;
            width: 100%;
            background: #f8f8f8; 
            height:100%;
        }
        header{
            display: flex;
            align-items: center;
            background: #e64340;
            padding: 12px;
            color: #fff;
            justify-content: space-between;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 44px;
            box-sizing: border-box;
        }
        header .title{
            text-align: center;
        }
        header .layui-icon{
            font-size: 18px;
        }
        main{
            padding: 44px 0 0;
        }
        .list{
            padding: 5px 10px;
        }
        .list .item{
            display: flex;
            align-items: center;
            justify-content: space-between;
            background: #fff;
            padding: 15px 10px;
            margin: 10px 0;
            border-radius: 5px;
        }
    </style>
</head>
<body ontouchstart>
    <div class="container" id="container">
        <header>
            <div>
                <i class="layui-icon layui-icon-left" onclick="history.back()"></i>
            </div>
            <div class="title">设置</div>
            <div class="add-btn"></div>
        </header>
        <main>
            <div class="list">
                <a class="item" href="./password.php">
                   <div>修改登录密码</div> 
                   <div>
                       <i class="layui-icon layui-icon-right"></i>   
                   </div>
                </a> 
                <div class="item">
                   <div>微信绑定</div> 
                   <?php if(!$user['openid']){ ?>
                   <a style="flex:1;display:flex;align-items:center"
                   href="./openid.php">
                       <div style="flex:1;text-align:right;margin: 0 10px;color:green">
                           未绑定
                       </div>
                       <div>
                           <i class="layui-icon layui-icon-right"></i>   
                       </div>
                   </a>
                   <?php } else {?>
                   <div><?=$user['nickname']?></div>
                   <?php } ?>
                </div>
            </div>
            <div style="margin: 20vh 15px 0;">
                <?php if(!$user['openid']){ ?>
                  <button class="layui-btn layui-btn-fluid layui-btn-danger layui-btn-lg loginout-btn" data-type="1">退出登录</button>
                <?php } else {?>
                    <button class="layui-btn layui-btn-fluid layui-btn-danger layui-btn-lg loginout-btn" data-type="2">解绑微信</button>
                <?php } ?>
            </div>
        </main>
    </div>
    <!-- 引入 layui.js -->
    <script src="./layui/layui.js"></script>
    <script>
        layui.use(['jquery','layer', 'form','code'], function(){
            var $ = layui.$,layer = layui.layer,form = layui.form,code = layui.code;
            $(document).on('click', '.loginout-btn', function() {
                var type = $(this).data("type")
                $.post("setting.php",{type},function(res){
                    res = JSON.parse(res)
                        layer.msg(res.msg);
                        if(res.code == 1){
                            setTimeout(function(){
                                if(type == 1){
                                   location.href = "login.php" 
                                } else {
                                  parent.location.reload()
                                }
                            },1000)
                        }
                    })
            })
        });
    </script>  
</body>
</html>